//
//  TYSTabBarController.m
//  勺子
//
//  Created by lanou on 16/8/8.
//  Copyright © 2016年 tang. All rights reserved.
//

#import "TYSTabBarController.h"
#import "TYSNavigationController.h"
#import "EssenceViewController.h"
#import "NewViewController.h"
#import "PostViewController.h"
#import "MeViewController.h"



@interface TYSTabBarController ()
@property (nonatomic, strong) UIView *TYSTabBar;
@property (nonatomic, assign) BOOL isTabBarShowing;

@end

@implementation TYSTabBarController



#pragma mark - 添加childVC
- (void)addChildVC {
    EssenceViewController *essence = [[EssenceViewController alloc] init];
    [self addOneChildViewController:essence];
    NewViewController *new = [[NewViewController alloc] init];
    [self addOneChildViewController:new];
    PostViewController *post = [[PostViewController alloc] init];
    [self addOneChildViewController:post];
    MeViewController *me = [[MeViewController alloc] init];
    [self addOneChildViewController:me];
}



// 添加一个childVC
- (void)addOneChildViewController:(UIViewController *)vc {
    TYSNavigationController *navi = [[TYSNavigationController alloc] initWithRootViewController:vc];
    [self addChildViewController:navi];
}

#pragma mark - 布局
- (void)setupMainView {
    
    CGPoint btnCenter = CGPointMake(screenWidth / 2, screenHeight - 35);
    
    self.TYSTabBar = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 120)];
    self.TYSTabBar.center = btnCenter;
    self.TYSTabBar.clipsToBounds = NO;
    self.TYSTabBar.transform = CGAffineTransformRotate(self.TYSTabBar.transform, M_PI);
    self.TYSTabBar.alpha = 0;
    [self.view addSubview:self.TYSTabBar];
    
//    NSArray *titleArray = @[@"精华", @"最新", @"发帖", @"我"];
    CGPoint tabBarCenter = CGPointMake(100, 60);
    for (NSInteger i = 0;i < 4; i++) {
        UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [btn setImage:[UIImage imageNamed:[NSString stringWithFormat:@"tabBar_btn%ld.png", (long)i]] forState:UIControlStateNormal];
//        [btn setTitle:titleArray[i] forState:UIControlStateNormal];
        btn.frame = CGRectMake(0, 0, 40, 40);
        switch (i) {
            case 0:
                btn.center = CGPointMake(tabBarCenter.x - 80, tabBarCenter.y - 25);
                break;
            case 1:
                btn.center = CGPointMake(tabBarCenter.x - 30, tabBarCenter.y - 60);
                break;
            case 2:
                btn.center = CGPointMake(tabBarCenter.x + 30, tabBarCenter.y - 60);
                break;
            case 3:
                btn.center = CGPointMake(tabBarCenter.x + 80, tabBarCenter.y - 25);
                break;
            default:
                break;
        }
        btn.tag = i + 1000;
        [btn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
        [self.TYSTabBar addSubview:btn];
    }
    
    UIButton *happBbtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [happBbtn setImage:[UIImage imageNamed:@"happy.png"] forState:UIControlStateNormal];
    happBbtn.frame = CGRectMake(0, 0, 50, 50);
    happBbtn.center = btnCenter;
    [happBbtn addTarget:self action:@selector(showTabBar) forControlEvents:UIControlEventTouchUpInside];
    _isTabBarShowing = NO;
    [self.view addSubview:happBbtn];
}

- (void)showTabBar {
    
    if (_isTabBarShowing) {
        // 隐藏tabBar
        [UIView animateWithDuration:0.5 animations:^{
            self.TYSTabBar.transform = CGAffineTransformRotate(self.TYSTabBar.transform, M_PI);
            self.TYSTabBar.alpha = 0;
        }];
        _isTabBarShowing = NO;
    } else {
        [UIView animateWithDuration
         :0.5 animations:^{
            self.TYSTabBar.transform = CGAffineTransformRotate(self.TYSTabBar.transform, M_PI);
            self.TYSTabBar.alpha = 1;
        }];
        _isTabBarShowing = YES;
    }
}

- (void)btnAction:(UIButton *)button {
    self.selectedIndex = button.tag - 1000;
}


#pragma mark - original
- (void)viewDidLoad {
    [super viewDidLoad];
    [self addChildVC];
    [self setupMainView];
}

- (void)viewWillAppear:(BOOL)animated {
    [self.tabBar setHidden:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}






@end
